<template>
  <el-dialog v-model="dialogVisible" title="管理对话记录" width="700" :before-close="close">
    <el-table :data="list" height="500" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="模型" prop="aiModel"></el-table-column>
      <el-table-column label="创建时间" prop="createTime"></el-table-column>
    </el-table>
    <!--
    <el-pagination
        v-model:current-page="pageConfig.currentPage"
        v-model:page-size="pageConfig.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="sizes, prev, pager, next"
        :total="list.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    -->
    <div class="c-more-operate">
      <el-button type="primary" @click="remove">删除</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { computed, ref, watch } from 'vue'
import { getAiList, removeContentAll } from '@/request/ai.js'
import {ElMessage, ElMessageBox} from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'remove'])

const dialogVisible = computed({
  get: () => props.modelValue,
  set: () => emit('update:modelValue', false)
})

watch(() => props.modelValue, (val) => {
  val && getList()
})

const list = ref([])
const pageConfig = ref({
  currentPage: 1,
  pageSize: 10
})
const getList = async () => {
  const { data } = await getAiList()
  selections.value = []
  list.value = data
}
const handleSizeChange = (value) => {
  console.log(value)
}
const handleCurrentChange = (value) => {
  console.log(value)
}

const selections = ref([])
const handleSelectionChange = (value) => {
  selections.value = value
}

const remove = async () => {
  const sessionIds = selections.value.map(item => item.id)
  await ElMessageBox.confirm(
      '确定删除吗?',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
  await removeContentAll({ sessionIds })
  ElMessage.success('删除成功')
  await getList()
  emit('remove')
}

const close = () => {
  dialogVisible.value = false
  selections.value = []
}
</script>

<style lang="less">
.c-more-operate {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>